package app.common.utils.dingtalk;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiAttendanceGetupdatedataRequest;
import com.dingtalk.api.response.OapiAttendanceGetupdatedataResponse;
import lombok.SneakyThrows;

import java.util.Date;

/**
 * <b>考勤工具类</b>
 * @author feizns
 * @since 2023/3/15
 */
public class AttendanceUtils {

    private final Auth2Utils auth2Utils;

    public AttendanceUtils(Auth2Utils auth2Utils) {
        this.auth2Utils = auth2Utils;
    }

    /**
     * 获取指定用户指定考勤日期的考勤详情
     *
     * @param userid 用户编号
     * @param date   {@link Date}
     * @return {@link OapiAttendanceGetupdatedataResponse.AtCheckInfoForOpenVo}
     * @link <a href="https://open.dingtalk.com/document/orgapp/obtain-the-attendance-update-data">获取用户考勤数据</a>
     */
    @SneakyThrows
    public OapiAttendanceGetupdatedataResponse.AtCheckInfoForOpenVo getUpdateData(String userid, Date date) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getupdatedata");
        OapiAttendanceGetupdatedataRequest req = new OapiAttendanceGetupdatedataRequest();
        req.setUserid(userid);
        req.setWorkDate(date);
        OapiAttendanceGetupdatedataResponse rsp;
        rsp = client.execute(req, auth2Utils.getAccessToken());
        if ( !rsp.isSuccess() )
            throw new RuntimeException(rsp.getBody());
        return rsp.getResult();
    }

//    CJtUtils

}
